# !/usr/bin/env python3
# -*- coding: utf-8 -*-

import ctypes

c_uint32 = ctypes.c_uint32


class ParaSimTbl_bits(ctypes.LittleEndianStructure):
    _fields_ = [
        # entry1
        ("schedule_period", c_uint32, 11),
        ('tmp8', c_uint32, 21),
        # entry2
        ("cycle_start_cyclen", c_uint32, 32),
        # entry3
        ("time_slot_length", c_uint32, 11),
        ('tmp10', c_uint32, 21),
    ]


class ParaSimTbl_bytes(ctypes.LittleEndianStructure):
    _fields_ = [
        ('entry8', c_uint32, 32),
        ('entry9', c_uint32, 32),
        ('entry10', c_uint32, 32),
    ]


class ParaTbl_union(ctypes.Union):
    _fields_ = [
        ("bit", ParaSimTbl_bits),
        ("byte", ParaSimTbl_bytes)
    ]


def _init_para_sim_tbl(schedule_period: int,
                   cyclelen: int,
                   time_slot_length: int,
                   ):
    para_tbl = ParaTbl_union()
    para_tbl.bit.schedule_period = schedule_period
    para_tbl.bit.tmp8 = 0
    para_tbl.bit.cycle_start_cyclen = cyclelen
    para_tbl.bit.time_slot_length = time_slot_length
    para_tbl.bit.tmp10 = 0

    return para_tbl


class ParaSimTblEachNode:
    def __init__(self,
                 node_id: int,
                 src_node: int,
                 schedule_period: int,
                 cyclelen: int,
                 time_slot_len: int,
                 node_type: str,
                 ) -> None:
        self.node_id = node_id
        self.tbl_each_node = _init_para_sim_tbl(schedule_period,
                                                cyclelen,
                                                time_slot_len)
        self.node_type = node_type
        self.src_node = src_node


class ParaSimTbl:
    def __init__(self):
        self.tbl_set = []

    def append_tbl(self,
                   tbl: ParaSimTblEachNode) -> None:
        self.tbl_set.append(tbl)
